package com.example.exp6;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    EditText username = null;
    EditText password = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.layout);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
//            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
//            return insets;
//        });
    }

    public void register(View v){
        String usernameText = username.getText().toString().trim();
        String passwordText = password.getText().toString().trim();
        // 输入验证
        if (usernameText.isEmpty() || passwordText.isEmpty()) {
            // 显示错误消息，例如使用 Toast
            Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String info = usernameText + ':' + passwordText;
        FileOutputStream fos = null;
        try {
            fos = openFileOutput("info.txt", MODE_PRIVATE);
            fos.write(info.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
        Log.e("register", "register: ");
    }

    public void login(View v){
        String usernameText = username.getText().toString().trim();
        String passwordText = password.getText().toString().trim();
        // 输入验证
        if (usernameText.isEmpty() || passwordText.isEmpty()) {
            // 显示错误消息，例如使用 Toast
            Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String[] info;
        try {
            FileInputStream fis = openFileInput("info.txt");
            byte[] buffer =  new byte[fis.available()];
            fis.read(buffer);
            info = new String(buffer).split(":");
        Log.e("llllll", info[0] + ' ' + info[1]);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Log.e("llllll", usernameText + ' ' + passwordText);

        if(usernameText.equals(info[0]) && passwordText.equals(info[1])){
            Intent intent = new Intent(MainActivity.this, MainActivity2.class);
            startActivity(intent);
        }
        else{
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("登录失败");
            builder.setMessage("是否继续");
            builder.setPositiveButton("重新输入", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    finish();
                }
            });
            builder.show();

        }

    }

    public void cancel(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("是否退出");
        builder.setPositiveButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.setNegativeButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        });
        builder.show();
    }
}